package cn.tsmchina.one.dao.order;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import cn.tsmchina.one.dao.BaseDaoSupport;
import cn.tsmchina.one.domain.order.Order;
import cn.tsmchina.one.util.C;
import cn.tsmchina.one.util.DateUtil;



@Repository
public class OrderDaoImpl extends BaseDaoSupport<Order> implements OrderDao
{

	@Override
	public Long addOrder(Order order)
	{
		return (Long)add(order);
		
	}

	@Override
	public List<Order> getQueryOrders(Map<String, String> parameter,int page, long customerId)
	{
		StringBuilder sb = new StringBuilder("from Order o where 1=1");
		Map<String, Object> hqlMap = query(parameter, sb);
		//sb.append(" and o.customerId = ").append(customerId);
		sb.append(" order by o.date desc");
		return super.lists(sb.toString(), hqlMap,page,C.Paging.SIZE);
	}

	private Map<String, Object> query(Map<String, String> parameter, StringBuilder sb)
	{
		Map<String, Object> hqlMap = new HashMap<>();
		String phone  = parameter.get("phone");
		String startDate = parameter.get("start_date");
		String endData = parameter.get("end_date");
		String orderNo = parameter.get("order_no");
		String payType = parameter.get("pay_way");
		String payStatus = parameter.get("pay_status");
		String deliveryStatus = parameter.get("delivery_status");
		String toAddress = parameter.get("toAddress");
		System.out.println(toAddress);
		if(!StringUtils.isEmpty(phone))
		{
			sb.append(" and o.tel = :tel");
			hqlMap.put("tel",phone);
		}
		if(!StringUtils.isEmpty(startDate))
		{
			sb.append(" and o.date >= :startDate");
			hqlMap.put("startDate",DateUtil.parse(startDate,"yyyy-MM-dd hh:mm:ss"));
		}
		if(!StringUtils.isEmpty(endData))
		{
			sb.append(" and o.date <= :endDate");
			hqlMap.put("endDate", DateUtil.parse(endData,"yyyy-MM-dd hh:mm:ss"));
		}
		if(!StringUtils.isEmpty(orderNo))
		{
			sb.append(" and o.orderId = :orderId");
			hqlMap.put("orderId", Long.parseLong(orderNo.trim()));
		}
		if(!StringUtils.isEmpty(payType))
		{
			sb.append(" and o.payType = :payType");
			hqlMap.put("payType", Integer.parseInt(payType));
		}
		if(!StringUtils.isEmpty(payStatus))
		{
			sb.append(" and o.payStatus = :payStatus");
			hqlMap.put("payStatus",Integer.parseInt(payStatus));
		}
		if(!StringUtils.isEmpty(deliveryStatus))
		{
			sb.append(" and o.sendStatus = :sendStatus");
			hqlMap.put("sendStatus",Integer.parseInt(deliveryStatus));
		}
		if(!StringUtils.isEmpty(toAddress))
		{
			if(toAddress.equals("red"))
				toAddress = "红客会";
			sb.append(" and o.toAddress = :toAddress");
			hqlMap.put("toAddress", toAddress);
		}
		return hqlMap;
	}

	@Override
	public int getTotal(Map<String, String> parameter, long customerId)
	{
		StringBuilder sb = new StringBuilder("select count(*) from Order o where 1=1");
		Map<String, Object> hqlMap = query(parameter, sb);
		//sb.append(" and o.customerId = ").append(customerId);
		return  super.count(sb.toString(), hqlMap);
	}

	@Override
	public void updatePayStatus(Long orderId)
	{
		String hql = "update from Order o set o.payStatus = ? where o.orderId = ?";
		executeUpdate(hql, C.PayStatus.yes,orderId);
	}

	@Override
	public void updateSendStatus(long orderId, int status)
	{
		String hql = "update from Order o set o.sendStatus = ? where o.orderId = ?";
		executeUpdate(hql, status,orderId);
	}

	@Override
	//获取我最近5个订单
	public List<Order> getUserOrders(Long userId, long customerId) {
		//List<Order> olist = null ;
		if(null!=userId){
			String hql = "from Order o where o.customerId=? and o.userId = '"+userId+"' order by o.date desc";
			return super.lists(hql, new Object[]{customerId});
		}
		
		return null;
	}

	@Override
	public void deleteOrder(long orderId) {
		super.delete(orderId);
	}

	@Override
	public List<Order> getUserOrders(Long userId, int page, long customerId) {
		if(null!=userId){
			String hql = "from Order o where o.customerId=? and o.userId = '"+userId+"' order by o.date desc";
			return super.lists(hql, page, C.Paging.FRONT_PAGE_SIZE, new Object[]{customerId});
		}
		
		return null;
	}
	
	@Override
	public List<Order> getOrdersByName(String name) {
		if(null != name){
			String hql = "from Order o where o.toAddress='" + name + "'";
			return super.lists(hql, new Object[]{});
		}
		return null;
	}
	@Override
	public int getTotalNum() {
		String hql = "select count(*) from Order o where 1=1";
		return super.count(hql, new Object[]{});
	}
}
